package com.sunbim.rsrv.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sunbim.rsrv.pojo.RiskRocky;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 * 本体劣化风险 Mapper 接口
 * </p>
 *
 * @author yuhui
 * @since 2021-12-30
 */
@Mapper
public interface RiskRockyMapper extends BaseMapper<RiskRocky> {
    @Select("select count(1) from risk_rocky where id=#{id}")
    boolean idExist(int id);

    @Select("select score.rownum " +
            "from (SELECT @rownum := @rownum + 1 as rownum, " +
            "             t2.relics_id           as relics_id " +
            "      FROM (SELECT @rownum := 0) t1, " +
            "           (SELECT relics_id, json_extract (${column}, '$.score') score " +
            "            FROM risk_rocky " +
            "            where user_id = #{userId} " +
            "            ORDER BY score DESC, update_time DESC) t2) score " +
            "where relics_id = #{relicsId};")
    Integer getRank(Integer userId, Integer relicsId, String column);
}
